<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests\admin\UserRequest;
use App\Model\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;


class UserController extends Controller
{
    /**访问角色管理页面
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        
        $data = User::all();
        return view('admin.user.add');
    }

    /** 访问管理员列表
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {   
       
        // $data = User::all();

        // dd($data);
        // dd($request->all());
         $data = DB::table('user')
                ->where(function($query)use($request){
                    if($request->input('keywords')){
                        $query->where('name','like','%'.$request->input('keywords').'%')
                              ->orwhere('email','like','%'.$request->input('keywords').'%')
                              ->orwhere('phone','like','%'.$request->input('keywords').'%');
                    }
                })
                ->paginate($request->input('num',5));
        // dd($data);
                 return view('admin.user.list',['data'=>$data,'request'=>$request->all()]);
       
    }

    /** 执行添加操作
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(UserRequest $request)
    {   
        
         //插入数据库
        $data =$request->except('_token','repass');
        //密码加密
        $data['pass'] = Hash::make($data['pass']);
        $data = User::create($data);

        if($data){
            return redirect('/user/create')->with('success','添加成功');
        }else{
            return back()->with('error','添加失败');
        }
    }

    /**  加载编辑页面
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

        $data = User::find($id);
        return view('admin.user.update',['data'=>$data]);
    }

    /** 加载编辑页面
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // echo 123;
        $data = User::find($id);
        // $data['pass'] = Hash::check($data['pass']);
        return view('admin.user.edit',['data'=>$data]);
        
    }

    /** 执行修改操作
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {       

        //字段验证
        $this->validate($request, [
            'name' => 'required',
            'phone' => 'required',
            'email' => 'required|email',
         ],[
            'name.required'=>'账号不能为空',
            'pass.required'=>'密码不能为空',
            'phone.required'=>'手机号码不能为空',
            'email.required'=>'邮箱不能为空',
            'email.email'=>'邮箱格式不正确',

         ]);
        $data = User::where('id','=',$id)->update($request->except('_token','_method'));  
        if($data){

             return redirect('/user/create')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
       
    }

    /**执行删除操作
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        
    }

    //删除操作
    public function del($id)
    {
        $del = User::find($id)->delete();
        return redirect('/user/create')->with('success','删除成功');
    }
}
